<?php
function wp_pagenavi( $before = '', $after = '', $ul_class = 'paginator', $total_p = true, $ajaxing = false ) {
	global $wpdb, $wp_query;
	if ( !is_single() ) {
		$posts_per_page = intval(get_query_var('posts_per_page'));
		$paged = intval(get_query_var('paged'));

        $pagenavi_options = array();
        $pagenavi_options['pages_text'] = __('Page %CURRENT_PAGE% of %TOTAL_PAGES%','wp-pagenavi');
        $pagenavi_options['current_text'] = '%PAGE_NUMBER%';
        $pagenavi_options['page_text'] = '%PAGE_NUMBER%';
        $pagenavi_options['first_text'] = __('First','wp-pagenavi');
        $pagenavi_options['last_text'] = __('Last','wp-pagenavi');
        $pagenavi_options['prev_text'] = '<';
        $pagenavi_options['next_text'] = '>';
        $pagenavi_options['dotright_text'] = __('...','wp-pagenavi');
        $pagenavi_options['dotleft_text'] = __('...','wp-pagenavi');
        $pagenavi_options['num_pages'] = 5;
        $pagenavi_options['always_show'] = 0;
		
		if( function_exists('of_get_option') &&
            of_get_option('layout_paginator_show_all_checkbox', false) )
        {
			$pagenavi_options['num_pages'] = 9999;
		}
        
        $numposts = $wp_query->found_posts;
		$max_page = $wp_query->max_num_pages;
        
		if(empty($paged) || $paged == 0) {
			$paged = 1;
		}
        
		$pages_to_show = intval($pagenavi_options['num_pages']);
		$pages_to_show_minus_1 = $pages_to_show-1;
		$half_page_start = floor($pages_to_show_minus_1/2);
		$half_page_end = ceil($pages_to_show_minus_1/2);
		$start_page = $paged - $half_page_start;
		if($start_page <= 0) {
			$start_page = 1;
		}
		$end_page = $paged + $half_page_end;
		if(($end_page - $start_page) != $pages_to_show_minus_1) {
			$end_page = $start_page + $pages_to_show_minus_1;
		}
		if($end_page > $max_page) {
			$start_page = $max_page - $pages_to_show_minus_1;
			$end_page = $max_page;
		}
		if($start_page <= 0) {
			$start_page = 1;
		}
        
        if( $ajaxing ) {
            add_filter( 'get_pagenum_link', 'dt_ajax_paginator_filter', 10, 1);
        }
        
		if($max_page > 1 || intval($pagenavi_options['always_show']) == 1) {
			
            echo $before."\n";
            echo '<div class="'. $ul_class. '">'."\n";
			
            if( $paged > 1 ) {
                echo '<a href="'.esc_url( get_pagenum_link() ). '">';
                echo '<span>'. $pagenavi_options['first_text']. '</span>';
                echo '</a>';
            }
                        
            if ( $start_page > 1 && $pages_to_show < $max_page ) {   
                if(!empty($pagenavi_options['dotleft_text'])) {
                    echo '<span>'.$pagenavi_options['dotleft_text'].' </span>';
                }
            }

            for($i = $start_page; $i <= $end_page; $i++) {						
                if($i == $paged) {
                    //$current_page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['current_text']);
                    $current_page_text = number_format_i18n($i);
                    echo '<a class="act" href="'.esc_url(get_pagenum_link($i)).'">';
                    echo "<span>{$current_page_text}</span>";
                    echo '</a>';
				} else {
                    //$page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['page_text']);
                    $page_text = number_format_i18n($i);
                    echo '<a href="'.esc_url(get_pagenum_link($i)).'" title="'.$page_text.'">';
                    echo "<span>{$page_text}</span>";
                    echo '</a>';
                }
            }
            
            if ( $end_page < $max_page ) {
                if(!empty($pagenavi_options['dotright_text'])) {
                    echo '<span>'.$pagenavi_options['dotright_text'].'</span>';
                }
            }
            
            if( $paged < $max_page ) {
                echo '<a href="'. esc_url( get_pagenum_link($max_page) ). '">';
                echo '<span>'. $pagenavi_options['last_text']. '</span>';
                echo '</a>';
            }
            
            if( $total_p ) {
                $pages_text = str_replace(
                    "%CURRENT_PAGE%",
                    number_format_i18n($paged),
                    $pagenavi_options['pages_text']
                );
                $pages_text = str_replace(
                    "%TOTAL_PAGES%",
                    number_format_i18n($max_page),
                    $pages_text
                );
                    
                echo '<div class="counter">';
                
                echo "<span>{$pages_text}</span>";
               
                // add some stuff
                add_filter( 'next_posts_link_attributes', 'dt_next_posts_link_attr' );
                add_filter( 'previous_posts_link_attributes', 'dt_previous_posts_link_attr' );
                if( $link = get_previous_posts_link('') ) {
                    echo $link;
                }
                
                if( $link = get_next_posts_link('', $max_page) ) {
                    echo $link;
                }
                // remove stuff for our safety
                remove_filter( 'next_posts_link_attributes', 'dt_next_posts_link_attr' );
                remove_filter( 'previous_posts_link_attributes', 'dt_previous_posts_link_attr' );
                
                echo '</div>';
            }
            
			echo '</div>'. "\n". $after. "\n";
		}
        
        remove_filter( 'get_pagenum_link', 'dt_ajax_paginator_filter', 10, 1);
	}
}
